package com.candy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.candy.bean.Userinfo;
import com.candy.service.IUserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@CrossOrigin
@RestController
public class UserInfoController {
    @Autowired
    IUserinfoService userInfoService;

//    @RequestMapping("/selectUserInfoes")
//    public List<Userinfo> selectUserInfoes(Userinfo userInfo){
//        return userInfoService.selectUserInfoes(userInfo);
//    }

    @RequestMapping("/editUserInfo")
    public int editUsertInfo(Userinfo userInfo){
       boolean ok= userInfoService.saveOrUpdate(userInfo);
       if(ok){
           return 1;}
       else {
        return 0;}
    }

    /**
     * 分页查询获取用户信息列表
     * @param pageNum 当前页
     * @param pageSize 每页显示的记录数
     * @param userInfo 条件查询
     * @return 包含分页信息的用户列表
     */
    @RequestMapping("/selectUserInfoesByPage")
    public IPage<Userinfo> selectUserInfoesByPage(Integer pageNum, Integer pageSize, Userinfo userInfo){
    IPage<Userinfo> pageInfo=new Page<>(pageNum,pageSize);
//   拼接查询条件
        QueryWrapper<Userinfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(userInfo.getUsername()!=null,"username",userInfo.getUsername())
                .like(userInfo.getUsername()!=null,"useraddress",userInfo.getUseraddress())
                .eq("isdelete",0)
                .orderByDesc("id");
    IPage<Userinfo> userPage=userInfoService.page(pageInfo,queryWrapper);
    userPage.getRecords();
        return userPage;
    }

}
